// import jsBridge from '~/assets/js/WebViewJavascriptBridge.js'
// import Vue from 'vue'


// Vue.use(jsBridge)

// console.log(jsBridge)



import Vue from 'vue'

var jsBridge= {
    install(Vue){
        Vue.prototype.jsBridge = {
        	init: function(callback) {
                var u = navigator.userAgent;
                var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
                if (!isiOS) {
                  // 判断当前是否存在可用的bridge对象
                  if (window.WebViewJavascriptBridge) {
                    callback(WebViewJavascriptBridge)
                  } else {
                    document.addEventListener(
                      'WebViewJavascriptBridgeReady',
                      function() {
                        callback(WebViewJavascriptBridge)
                      },
                      false
                    );
                  }
                } else {
                  if (window.WebViewJavascriptBridge) {
                    return callback(WebViewJavascriptBridge);
                  }
                  if (window.WVJBCallbacks) {
                    return window.WVJBCallbacks.push(callback);
                  }
                  window.WVJBCallbacks = [callback];
                  // 创建iframe 对象， 在网页中，iframe的用法非常多，主要应用于伪ajax、嵌套页面、页面重用。
                  var WVJBIframe = document.createElement('iframe');
                  // 修改 iframe 属性参数
                  WVJBIframe.style.display = 'none';
                  WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
                  document.documentElement.appendChild(WVJBIframe);
                  // 超时则移除iframe 对象
                  setTimeout(function() {
                    document.documentElement.removeChild(WVJBIframe)
                  }, 0)
                }
              },
              first: function() {
                var u = navigator.userAgent;
                var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
                console.log(u)
                if (!isiOS) {
                  console.log('noWebView')
                  var _this = this;
                  _this.init(function(bridge) {
                    bridge.init(function(message, responseCallback) {
                      responseCallback(data);
                    })
                  })
                }
              },
              registerHandler: function(name,fun) {
                var _this = this;
                _this.init(function(bridge) {
                  bridge.registerHandler(name,fun);
                })
              },
              callHandler: function(name, data, fun) {
                var _this = this;
                _this.init(function(bridge) {
                  bridge.callHandler(name, data, fun);
                })
              }
        };
    }
}

Vue.use(jsBridge);

